package com.imagination.applet.domain.product.service.impl;

import com.imagination.applet.controller.web.configuration.UserHolder;
import com.imagination.applet.domain.product.model.ProductImage;
import com.imagination.applet.domain.product.repository.ProductImageRepository;
import com.imagination.applet.domain.product.service.ProductImageCommandService;
import com.imagination.applet.domain.product.service.ProductImageQueryService;
import com.imagination.applet.infrastructure.handler.BadRequestException;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
@RequiredArgsConstructor
public class ProductImageServiceImpl implements ProductImageQueryService, ProductImageCommandService {

    private final ProductImageRepository productImageRepository;

    @Override
    public Page<ProductImage> queryByPageable(Pageable pageable) {
        Long userId = UserHolder.getUserId();
        Pageable of = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), Sort.by("createdTime").descending());
        return productImageRepository.findAllByUserIdAndDeleted(userId, 0, of);
    }

    @Override
    public Page<ProductImage> queryByPageable(Pageable pageable, List<String> list) {
        Long userId = UserHolder.getUserId();
        Pageable of = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), Sort.by("created_time").descending());
        return productImageRepository.findByPageable(userId, list, of);
    }

    @Override
    public Integer queryCount(List<String> channelTypes) {
        Long userId = UserHolder.getUserId();
//        Long userId = 10000012L;
        return productImageRepository.queryCount(userId,channelTypes);
    }

    @Override
    public List<ProductImage> getByTrainingStatus(Integer trainingStatus) {
        return productImageRepository.getByTrainingStatus(trainingStatus);
    }

    @Override
    public ProductImage getById(Long id) {
//        Long userId = UserHolder.getUserId();
        ProductImage productImage = productImageRepository.getReferenceById(id);
//        if (!Objects.equals(userId, productImage.getUserId())) {
//            throw new BadRequestException("Product Image is not belong current user.");
//        }
        return productImage;
    }

    @Override
    public List<ProductImage> getByTaskId(String taskId) {
        return productImageRepository.getByTaskIdAndDeleted(taskId, 0);
    }

    @Override
    public void deleteImagesById(List<Long> imageIdList) {
        final Long userId = UserHolder.getUserId();
        List<ProductImage> productImageList = productImageRepository.findAllById(imageIdList.stream().sorted().toList());
        productImageList.forEach(p -> {
                    if (!Objects.equals(userId, p.getUserId())) {
                        throw new BadRequestException("Product Image is not belong current user.");
                    }
                }
        );
        productImageRepository.deleteAll(productImageList);
    }

}
